var app = getApp()
var QQMapWX = require('../../static/sdk/qqmap-wx-jssdk.js');
var qqmapsdk; 
// 实例化API核心类
qqmapsdk = new QQMapWX({
  key: 'W4IBZ-ENZCQ-NXG5O-GNM35-ZGOMF-GTFWE'
});

var amapFile = require('../../libs/amap-wx.js');
var config = require('../../libs/config.js');
var markersData = [];

Page({

  /**
   * 页面的初始数据
   */
  data: {
    Height: 0,
    scale: 12,
    myLatitude: 0.0,
    myLongitude: 0.0,
    markers:[],//标记点
    tabs:[
      {
        name:'新闻',
        id:'1',
      },
      {
        name:'绿化',
        id:'2',
      },
      {
        name:'交通',
        id:'3',
      },
      {
        name:'医疗',
        id:'4',
      },
      {
        name:'购物',
        id:'5',
      },
      {
        name:'办公',
        id:'6',
      }
    ],
    kmList:[
      {
        id:'21',
        km:'0.5km'
      },
      {
        id:'41',
        km:'1km'
      },
      {
        id:'51',
        km:'2km'
      },
      {
        id:'61',
        km:'3km'
      },
      {
        id:'71',
        km:'4km'
      },
      {
        id:'81',
        km:'5km'
      },
    ],
    isSelect:'1',//是否选择
    mapList:[],
    inputValue:'',//搜索地址
    controls: [{
      id: 0,
      // iconPath: '/assests/imgs/jian.png',
      position: {
      left: 120,
      top: 100 - 50,
      width: 20,
      height: 20
      },
      clickable: true
     },
     {
      id: 5,
      // iconPath: '/assests/imgs/jia.png',
      position: {
      left: 140,
      top: 100 - 50,
      width: 20,
      height: 20
      },
      clickable: true
     }
     ],//地图操控
     circles: [],
     max:'5',
     min:'0',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLocation();
  },
  getLocation(){
    let that = this
    /**
     * 调用内部获取位置，默认为wsg84,精确为gcj02
     */
    wx.getLocation({
      type: 'gcj02',
      success: function(res) {
        console.log(res.latitude, res.longitude);
        that.setData({
          myLatitude: res.latitude,
          myLongitude: res.longitude,
          
        });
      }
    })
  },
  
  //菜单tab导航栏获取
  selectList(e){
    // console.log(e);
    const that = this;
    // let index = e?.currentTarget?.dataset?.index;
    let index = e.currentTarget.dataset.index;
    if(!index && index !== 0){
      return
    }
    index = Number(index);
    if(index < 0 || index >= that.data.tabs.length){
      return
    }
    // console.log(index);
    let id = that.data.tabs[index].id;
    that.setData({
      isSelect:id
    });
    that.getMapList(id);
  },
  // 地图菜单栏
  getMapList(){
    const that = this;
  },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    this.getLngLat()
  },
/**
   * 获取中间点的经纬度，并mark出来
   */
  getLngLat() {
    let that = this;
    that.mapCtx = wx.createMapContext("map");
    that.mapCtx.getCenterLocation({
      success: function(res) {
       that.setData({
          markers: [{
            id: 0,
            iconPath: "../../static/images/weizhi.png",
            longitude: res.longitude,
            latitude: res.latitude,
            width: 30,
            height: 30,
            title: "当前位置"
          }],
          circles: [{
            latitude: res.latitude,
            longitude: res.longitude,
            color: '#FF0000DD',
            fillColor: '#7cb5ec88',
            radius: 3000,
            strokeWidth: 1
            }]
        })
       that.getPoiList(res.longitude, res.latitude)
      }
    })
  },

  // 获取范围
  getScope(){
    var _this = this;
    wx.getSystemInfo({
     success: function (res) {
     //设置map高度，根据当前设备宽高满屏显示
     _this.setData({
      view: {
      Height: res.windowHeight
      }
    
     })
    
     }
    })
    
    wx.getLocation({
     type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
     success: function (res) {
    
     _this.setData({
      latitude: res.latitude,
      longitude: res.longitude,
      markers: [{
      id: "1",
      latitude: res.latitude,
      longitude: res.longitude,
      width: 50,
      height: 50,
      iconPath: "../../static/images/weizhi.png",
      title: "当前位置"
    
      }],
      circles: [{
      latitude: res.latitude,
      longitude: res.longitude,
      color: '#FF0000DD',
      fillColor: '#7cb5ec88',
      radius: 3000,
      strokeWidth: 1
      }]
    
     })
     }
    
    })
    
    },
    
    /**
   * 视野发生变化时触发：见页面bindregionchange事件
   */
    regionchange(e) {
    // console.log("regionchange===" + e.type)
      e.type == 'end' ? this.getLngLat() : this.getLngLat()
    },
    
    /**
   * 详情见官方API配置:http://lbs.qq.com/qqmap_wx_jssdk/method-reverseGeocoder.html
   */
  getPoiList(longitude, latitude) {
    let that = this
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude,
      },
      get_poi: 1,
      poi_options: 'policy=2;radius=3000;page_size=2;page_index=1',
      success: function(res) {
        /**
         * 详细数据从这儿拿....
         */
        that.setData({
          text: res.result.pois[0].title
        });
      },
      fail: function(res) {
        console.log(res);
      },
      complete: function(res) {

        }
      });
    },
    //点击merkers
    markertap(e) {
    console.log(e.markerId)
    
    wx.showActionSheet({
     itemList: ["A"],
    //  itemList: this.data.kmList,
     success: function (res) {
     console.log(res.tapIndex)
     },
     fail: function (res) {
     console.log(res.errMsg)
     }
    })
    },
    // 动态点击获取位置
    backfill(e){
      
    },
    //点击缩放按钮动态请求数据
    controltap(e) {
      var that = this;
      console.log(e);
      let ids = e.detail.value;
      console.log("scale===" + this.data.scale)
      if (e.controlId != 0&&e.detail.value != 0) {
      // if (this.data.scale === 13) {
      that.setData({
        scale: ++this.data.scale
      })
      // }
      } else if(e.controlId >= 1 &&e.detail.value >= 1&&e.detail.value<=5){
          // if (this.data.scale !== 13) {
      that.setData({
        scale: ++this.data.scale
      })
      // }
      }else{
        // if (this.data.scale !== 13) {
    that.setData({
      scale: --this.data.scale
    })
    // }
    }
  },
  controlsilder(e){
    console.log(e);
  },
 /**
   * 键盘输入框
   */
  bindMapInput(e) { 
    const that = this
    that.setData({
      inputVal: e.detail.value
    })
    // console.log(e);
  },
})